/**
 * 
 */
package leetCode;

/**
 * @author zhongfang
 *
 */
public class SquirrelSimulation {
	public int minDistance(int height, int width, int[] tree, int[] squirrel, int[][] nuts) {
		int[] d = new int[nuts.length];// 所有松子到树的距离
		int minDiff = Integer.MAX_VALUE;// 所有松子到松鼠的最小距离及该松子的下标
		int result = 0;
		for (int i = 0; i < nuts.length; i++) {
			d[i] = Math.abs(nuts[i][0] - tree[0]) + Math.abs(nuts[i][1] - tree[1]);// 松子到树的距离
			int disSqui = Math.abs(nuts[i][0] - squirrel[0]) + Math.abs(nuts[i][1] - squirrel[1]);// 松子到松鼠的距离
			int diff = disSqui - d[i];
			if (diff < minDiff) {
				minDiff = diff;
			}
			result += (2 * d[i]);
		}
		result += minDiff;
		return result;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SquirrelSimulation squirrelSimulation = new SquirrelSimulation();
		int heigt = 9;
		int wid = 9;
		int tree[] = { 7, 2 };
		int[] squ = { 7, 1 };
		int nut[][] = { { 3, 0 }, { 7, 7 }, { 8, 8 }, { 0, 8 }, { 2, 4 }, { 4, 5 }, { 3, 5 }, { 4, 2 }, { 1, 8 } };
		System.out.println(squirrelSimulation.minDistance(heigt, wid, tree, squ, nut));
	}

}
